package com.example.animationtest_1;

import android.animation.AnimatorInflater;
import android.animation.ObjectAnimator;
import android.animation.PropertyValuesHolder;
import android.animation.TypeEvaluator;
import android.animation.ValueAnimator;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.animation.LinearInterpolator;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;

public class AnimatorActicity extends AppCompatActivity implements View.OnClickListener{
    private Button mButton;
    private ImageView mImage;
    private TextView mText;
    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_animator);
        mButton=findViewById(R.id.button);
        mImage=findViewById(R.id.image);
        mButton.setOnClickListener(this);
        mText=findViewById(R.id.text);
    }

    public void onClick(View v){
        int id=v.getId();
        if (id==R.id.button){
//            initAnimator();
//            initAnimatorJava();
//            initAnimatorJava2();
//            initAnimatorJava3();
//            initObjectAnimator();
//            initObjectAnimator2();
            initObjectAnimator3();
        }
    }

    private void initAnimator(){
        ValueAnimator valueAnimator=(ValueAnimator) AnimatorInflater.loadAnimator(this,R.animator.animator);
        valueAnimator.setTarget(mImage);
        valueAnimator.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {
            @Override
            public void onAnimationUpdate(@NonNull ValueAnimator animation) {
                float value=(float) animation.getAnimatedValue();
                Log.e("test123","==============="+value);
//                mImage.setRotation(value*360);
//                mImage.setRotationX(value*360);
//                mImage.setRotationY(value*360);
//                mImage.setAlpha(1-value);
//                mImage.getLayoutParams().width= (int) (mImage.getLayoutParams().width*value);
//                mImage.requestLayout();
                mImage.setTranslationX(value*100);
                mImage.setTranslationY(value*300);
                mText.setText(value+"");
            }
        });
        valueAnimator.start();
    }

    private void initAnimatorJava(){
        ValueAnimator valueAnimator=ValueAnimator.ofFloat(1,0,1,0,1);
        valueAnimator.setDuration(4000);
        valueAnimator.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {
            @Override
            public void onAnimationUpdate(@NonNull ValueAnimator animation) {
                float value=(float) animation.getAnimatedValue();
                Log.e("test123","==============="+value);
//                mImage.setScaleX(value);
//                mImage.setScaleY(value);
                mImage.setAlpha(value);
            }
        });
        valueAnimator.setInterpolator(new LinearInterpolator());
        valueAnimator.start();
    }

    private void initAnimatorJava2(){
        ValueAnimator valueAnimator=ValueAnimator.ofArgb(
                0xffff0000,0xff00ff00,0xff0000ff,0xffffffff,0xff000000);
        valueAnimator.setDuration(6000);
        valueAnimator.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {
            @Override
            public void onAnimationUpdate(@NonNull ValueAnimator animation) {
                int value= (int) animation.getAnimatedValue();
                mText.setBackgroundColor(value);
            }
        });
        valueAnimator.setInterpolator(new LinearInterpolator());
        valueAnimator.start();
    }

    private void initAnimatorJava3(){
        Man boy=new Man("小王",18);
        Man old=new Man("老王",80);
        ValueAnimator valueAnimator=ValueAnimator.ofObject(new TypeEvaluator() {
            @Override
            public Object evaluate(float fraction, Object startValue, Object endValue) {
                Man boy= (Man) startValue;
                Man old= (Man) endValue;
                int age= (int) (boy.getAge()+(old.getAge()- boy.getAge())*fraction);
                String name;
                if (age<30){
                    name="小王";
                }else if(age<60){
                    name="王哥";
                }else {
                    name="老王";
                }
                Man man=new Man(name,age);
                return man;
            }
        },boy,old);

        valueAnimator.setDuration(6000);
        valueAnimator.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {
            @Override
            public void onAnimationUpdate(@NonNull ValueAnimator animation) {
                Man man= (Man) animation.getAnimatedValue();
                mText.setText(man.toString());
            }
        });
        valueAnimator.setInterpolator(new LinearInterpolator());
        valueAnimator.start();
    }

    private void initObjectAnimator(){
//        ObjectAnimator  objectAnimator=ObjectAnimator.ofFloat(
//                mImage,"alpha",1,0,1,0,1);
//        ObjectAnimator  objectAnimator=ObjectAnimator.ofFloat(
//                mImage,"rotationY",360,180,270,90,360);
        ObjectAnimator  objectAnimator=ObjectAnimator.ofFloat(
                mImage,"translationY",0,300,-270,190,-360,0);
        objectAnimator.setInterpolator(new LinearInterpolator());
        objectAnimator.setDuration(2500);
        objectAnimator.start();
    }

    private void initObjectAnimator2(){
        Man man=new Man("ggg",0);
        ObjectAnimator objectAnimator=ObjectAnimator.ofInt(
                man,"age",0,100);
        objectAnimator.setInterpolator(new LinearInterpolator());
        objectAnimator.setDuration(4000);
        objectAnimator.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {
            @Override
            public void onAnimationUpdate(@NonNull ValueAnimator animation) {
                mText.setText(man.toString());
            }
        });
        objectAnimator.start();
    }

    private void initObjectAnimator3(){
        PropertyValuesHolder rotationHolder=PropertyValuesHolder.ofFloat(
                "Rotation",0,360,0,-360,0);
        PropertyValuesHolder colorHolder=PropertyValuesHolder.ofInt(
                "BackgroundColor",0xffff0000,0xff00ff00,0xff0000ff,0xff000000,0xffffffff);
        PropertyValuesHolder scaleXHolder=PropertyValuesHolder.ofFloat(
                "ScaleX",1,0.4f,1.5F,1.2F,0.3F,1.0F);
        PropertyValuesHolder scaleYHolder=PropertyValuesHolder.ofFloat(
                 "scaleY",1,2,0.5f,1.6f,0.4f,1.0f);
        PropertyValuesHolder alphaHolder=PropertyValuesHolder.ofFloat(
                "Alpha",1,0,1,0,0.7f,0.2f,1);
        PropertyValuesHolder translationYHolder=PropertyValuesHolder.ofFloat(
                "TranslationY",0,300,0,-300,0,300,0);

        ObjectAnimator objectAnimator=ObjectAnimator.ofPropertyValuesHolder(
                mText,rotationHolder,translationYHolder,scaleXHolder,scaleYHolder,alphaHolder,colorHolder);
        objectAnimator.setDuration(2000);
        objectAnimator.setInterpolator(new LinearInterpolator());
        objectAnimator.start();
    }
}
